package com.quasar.hpatient.module.comm_login;

import android.content.Intent;
import android.text.TextUtils;
import android.widget.EditText;

import com.quasar.hpatient.R;
import com.quasar.hpatient.api.HttpClient;
import com.quasar.hpatient.api.HttpParams;
import com.quasar.hpatient.bean.comm_login.LoginBean;
import com.quasar.hpatient.module.comm_compile.CompileActivity;
import com.quasar.hpatient.module.comm_main.MainActivity;

import java.util.HashMap;

import io.reactivex.Observable;
import lib.quasar.base.frame.BasePresenter;
import lib.quasar.base.frame.OnModelAcceptChangeListener;
import lib.quasar.context.BaseApp;
import lib.quasar.context.BaseConstant;
import lib.quasar.db.manager.DBManager;
import lib.quasar.db.table.User;
import okhttp3.RequestBody;

/**
 * description: 登录
 * created by kalu on 2018/4/2 15:31
 */
public class LoginPresenter implements BasePresenter {

    void loginAccount(final LoginView view, final EditText user, EditText pass) {

        if (null == view || null == user || null == pass) return;

        user.clearFocus();
        pass.clearFocus();

        final String userId = user.getText().toString().trim();
        if (TextUtils.isEmpty(userId)) {
            view.warning(BaseApp.getResource().getString(R.string.comm_login_hint_msg_account));
            return;
        }

        if (userId.length() < 3) {
            view.warning(BaseApp.getResource().getString(R.string.comm_login_hint_msg_account_lengh));
            return;
        }

        final String password = pass.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            view.warning(BaseApp.getResource().getString(R.string.comm_login_hint_msg_password));
            return;
        }

        final HashMap<String, Object> map = new HashMap<>();
        map.put(HttpParams.PASSWORD, password);
        map.put(HttpParams.USER_ID, userId);
        map.put(HttpParams.USER_TYPE, BaseConstant.TWO);
        final RequestBody params = createParams(map, HttpParams.DATA_TYPE_13);
        final Observable observable = HttpClient.getSingleHolder().getHttpService().login(params);

        request(observable, new OnModelAcceptChangeListener<LoginBean>() {
            @Override
            public void modelStart() {
                view.beginLoading();
            }

            @Override
            public void modelComplete() {
                view.closeLoading();
            }

            @Override
            public void modelSucc(LoginBean model) {

                final User user = DBManager.getInstance().syncGetUserModel();
                user.updateInfo(model);
                DBManager.getInstance().updataUserModel(user);

                view.toast(model.needCompile() ? "完善用户信息" : "登陆成功");
                final Class<?> clazz = model.needCompile() ? CompileActivity.class : MainActivity.class;
                final Intent intent = new Intent(BaseApp.getContext(), clazz);
                view.goMain(intent);
            }
        });
    }

    @Override
    public void recycler() {
    }
}
